#include <stdio.h>
#include "ch32v10x.h"
#include "config.h"
#include "mode.h"
#include "multi_button.h"
#include "debug.h"

int delayTime = 500;

uint8_t ledMode = LEDFLASH_1HZ;

void GPIO_ReadButtonLevel_INIT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Button;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void GPIO_ControlLedLevel_INIT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Led;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

uint8_t Read_Button_GPIO()
{
    return GPIO_ReadInputDataBit(GPIOA, GPIO_Button);
}

void Button_Callback(void *button)
{
    uint32_t btn_event_val;

    btn_event_val = get_button_event((struct Button *)button);

    switch(btn_event_val)
    {
        case PRESS_DOWN:
            printf("---> key1 press down! <---\r\n");
            switch(ledMode)
            {
            case LEDFLASH_1HZ:
                ledMode = LEDFLASH_2HZ;
                delayTime = 250;
                break;
            case LEDFLASH_2HZ:
                ledMode = LEDFLASH_3HZ;
                delayTime = 166;
                break;
            case LEDFLASH_3HZ:
                ledMode = LEDFLASH_1HZ;
                delayTime = 500;
                break;
            default:
                break;
            }
            break;
        default:
            break;
    }

}
